3 * Creates a RCCacheEntry from a RecentChange to use in EnhancedChangesList
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 use MediaWiki\Linker\LinkRenderer
;
24 class RCCacheEntryFactory
{
26 /* @var IContextSource */
35 private $linkRenderer;
38 * @param IContextSource $context
39 * @param string[] $messages
40 * @param LinkRenderer $linkRenderer
42 public function __construct(
43 IContextSource
$context, $messages, LinkRenderer
$linkRenderer
45 $this->context
= $context;
46 $this->messages
= $messages;
47 $this->linkRenderer
= $linkRenderer;
51 * @param RecentChange $baseRC
52 * @param bool $watched
54 * @return RCCacheEntry
56 public function newFromRecentChange( RecentChange
$baseRC, $watched ) {
57 $user = $this->context
->getUser();
58 $counter = $baseRC->counter
;
60 $cacheEntry = RCCacheEntry
::newFromParent( $baseRC );
62 // Should patrol-related stuff be shown?
63 $cacheEntry->unpatrolled
= ChangesList
::isUnpatrolled( $baseRC, $user );
65 $cacheEntry->watched
= $cacheEntry->mAttribs
['rc_type'] == RC_LOG ?
false : $watched;
66 $cacheEntry->numberofWatchingusers
= $baseRC->numberofWatchingusers
;
68 $cacheEntry->link
= $this->buildCLink( $cacheEntry );
69 $cacheEntry->timestamp
= $this->buildTimestamp( $cacheEntry );
71 // Make "cur" and "diff" links. Do not use link(), it is too slow if
72 // called too many times (50% of CPU time on RecentChanges!).
73 $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
75 $cacheEntry->difflink
= $this->buildDiffLink( $cacheEntry, $showDiffLinks, $counter );
76 $cacheEntry->curlink
= $this->buildCurLink( $cacheEntry, $showDiffLinks, $counter );
77 $cacheEntry->lastlink
= $this->buildLastLink( $cacheEntry, $showDiffLinks );
80 $cacheEntry->userlink
= $this->getUserLink( $cacheEntry );
82 if ( !ChangesList
::isDeleted( $cacheEntry, Revision
::DELETED_USER
) ) {
83 $cacheEntry->usertalklink
= Linker
::userToolLinks(
84 $cacheEntry->mAttribs
['rc_user'],
85 $cacheEntry->mAttribs
['rc_user_text'],
86 // Should the contributions link be red if the user has no edits (using default)
88 // Customisation flags (using default 0)
90 // User edit count (using default )
92 // do not wrap the message in parentheses
101 * @param RecentChange $cacheEntry
106 private function showDiffLinks( RecentChange
$cacheEntry, User
$user ) {
107 return ChangesList
::userCan( $cacheEntry, Revision
::DELETED_TEXT
, $user );
111 * @param RecentChange $cacheEntry
115 private function buildCLink( RecentChange
$cacheEntry ) {
116 $type = $cacheEntry->mAttribs
['rc_type'];
118 // New unpatrolled pages
119 if ( $cacheEntry->unpatrolled
&& $type == RC_NEW
) {
120 $clink = $this->linkRenderer
->makeKnownLink( $cacheEntry->getTitle() );
122 } elseif ( $type == RC_LOG
) {
123 $logType = $cacheEntry->mAttribs
['rc_log_type'];
126 $clink = $this->getLogLink( $logType );
128 wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
129 $clink = $this->linkRenderer
->makeLink( $cacheEntry->getTitle() );
131 // Log entries (old format) and special pages
132 } elseif ( $cacheEntry->mAttribs
['rc_namespace'] == NS_SPECIAL
) {
133 wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
137 $clink = $this->linkRenderer
->makeKnownLink( $cacheEntry->getTitle() );
143 private function getLogLink( $logType ) {
144 $logtitle = SpecialPage
::getTitleFor( 'Log', $logType );
145 $logpage = new LogPage( $logType );
146 $logname = $logpage->getName()->text();
148 $logLink = $this->context
->msg( 'parentheses' )
150 $this->linkRenderer
->makeKnownLink( $logtitle, $logname )
157 * @param RecentChange $cacheEntry
161 private function buildTimestamp( RecentChange
$cacheEntry ) {
162 return $this->context
->getLanguage()->userTime(
163 $cacheEntry->mAttribs
['rc_timestamp'],
164 $this->context
->getUser()
169 * @param RecentChange $recentChange
173 private function buildCurQueryParams( RecentChange
$recentChange ) {
175 'curid' => $recentChange->mAttribs
['rc_cur_id'],
177 'oldid' => $recentChange->mAttribs
['rc_this_oldid']
182 * @param RecentChange $cacheEntry
183 * @param bool $showDiffLinks
184 * @param int $counter
188 private function buildCurLink( RecentChange
$cacheEntry, $showDiffLinks, $counter ) {
189 $queryParams = $this->buildCurQueryParams( $cacheEntry );
190 $curMessage = $this->getMessage( 'cur' );
191 $logTypes = [ RC_LOG
];
193 if ( !$showDiffLinks ||
in_array( $cacheEntry->mAttribs
['rc_type'], $logTypes ) ) {
194 $curLink = $curMessage;
196 $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
197 $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
204 * @param RecentChange $recentChange
208 private function buildDiffQueryParams( RecentChange
$recentChange ) {
210 'curid' => $recentChange->mAttribs
['rc_cur_id'],
211 'diff' => $recentChange->mAttribs
['rc_this_oldid'],
212 'oldid' => $recentChange->mAttribs
['rc_last_oldid']
217 * @param RecentChange $cacheEntry
218 * @param bool $showDiffLinks
219 * @param int $counter
223 private function buildDiffLink( RecentChange
$cacheEntry, $showDiffLinks, $counter ) {
224 $queryParams = $this->buildDiffQueryParams( $cacheEntry );
225 $diffMessage = $this->getMessage( 'diff' );
226 $logTypes = [ RC_NEW
, RC_LOG
];
228 if ( !$showDiffLinks ) {
229 $diffLink = $diffMessage;
230 } elseif ( in_array( $cacheEntry->mAttribs
['rc_type'], $logTypes ) ) {
231 $diffLink = $diffMessage;
232 } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
233 $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
234 $pageTitle = Title
::newFromID( $rcCurId );
235 if ( $pageTitle === null ) {
236 wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
239 $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
240 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
242 $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
243 $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
250 * Builds the link to the previous version
252 * @param RecentChange $cacheEntry
253 * @param bool $showDiffLinks
257 private function buildLastLink( RecentChange
$cacheEntry, $showDiffLinks ) {
258 $lastOldid = $cacheEntry->mAttribs
['rc_last_oldid'];
259 $lastMessage = $this->getMessage( 'last' );
260 $type = $cacheEntry->mAttribs
['rc_type'];
261 $logTypes = [ RC_LOG
];
264 if ( !$showDiffLinks ||
!$lastOldid ||
in_array( $type, $logTypes ) ) {
265 $lastLink = $lastMessage;
267 $lastLink = $this->linkRenderer
->makeKnownLink(
268 $cacheEntry->getTitle(),
269 new HtmlArmor( $lastMessage ),
270 [ 'class' => 'mw-changeslist-diff' ],
271 $this->buildDiffQueryParams( $cacheEntry )
279 * @param RecentChange $cacheEntry
283 private function getUserLink( RecentChange
$cacheEntry ) {
284 if ( ChangesList
::isDeleted( $cacheEntry, Revision
::DELETED_USER
) ) {
285 $userLink = ' <span class="history-deleted">' .
286 $this->context
->msg( 'rev-deleted-user' )->escaped() . '</span>';
288 $userLink = Linker
::userLink(
289 $cacheEntry->mAttribs
['rc_user'],
290 $cacheEntry->mAttribs
['rc_user_text'],
291 ExternalUserNames
::getLocal( $cacheEntry->mAttribs
['rc_user_text'] )
303 private function getMessage( $key ) {
304 return $this->messages
[$key];